package com.yunhe.assist.controller;

import com.alibaba.fastjson.JSONObject;
import com.yunhe.assist.domain.Feedback;
import com.yunhe.assist.enums.EnumFeedbackUserType;
import com.yunhe.assist.model.FeedbackMobileVO;
import com.yunhe.assist.model.FeedbackQueryCondition;
import com.yunhe.assist.service.FeedbackService;
import com.yunhe.assist.util.MyHeaderUtil;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description feedback
 * @author dlz
 * @date 2021-11-09
 */
@Api(value = "Feedback", description = "feedback维护", tags = {"Model_Feedback"})
@RestController
@RequestMapping(value = "/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @ApiOperation(value = "提交反馈",notes = "1、必填项：真实园区reallyStationId， 虚拟园区virtualStationId，反馈内容content，反馈人person，反馈账号account。" +
            "2、小程序接口传入userType='company',用户id（userId） 反馈企业company，反馈的园区park；" +
            "3、app传入userType='park'")
    @PostMapping("/submit")
    public ResponseEntity<ResultObject> submit(@RequestBody Feedback feedback){
        feedbackService.createFeedback(feedback, myHeaderUtil.getHeaderUser());
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /*@ApiOperation("根据id删除feedback")
    @DeleteMapping("/deleteFeedback")
    public ResponseEntity<ResultObject> deleteFeedback(@RequestParam(value = "id",required = true) int id){
        feedbackService.deleteFeedback(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }*/

    /*@ApiOperation("更新feedback")
    @PostMapping("/updateFeedback")
    public ResponseEntity<ResultObject> updateFeedback(@RequestBody Feedback feedback){
        feedbackService.updateFeedback(feedback);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }*/

    @ApiOperation("根据id查询feedback")
    @GetMapping("/findFeedbackById")
    public ResponseEntity<ResultObject<Feedback>> findFeedbackById(@RequestParam(value = "id",required = true) int id){
        return new ResponseEntity<>(new ResultObject(feedbackService.findFeedbackById(id)),HttpStatus.OK);
    }

    @ApiOperation(value = "园区后台分页查询", notes = "content：反馈内容；replyStat：回复状态1：已回复，0未回复")
    @PostMapping("/listForPark/{page}/{size}")
    public ResponseEntity<ResultObject<Feedback>> listForPark(@RequestBody(required = false) Feedback feedback, @PathVariable("page") int page, @PathVariable("size") int size){
        feedback.setVirtualStationId(MyHeaderUtil.getVirtualStationId());
        feedback.setUserType(EnumFeedbackUserType.COMPANY.getKey());
        PageResult<Feedback> pageResult = feedbackService.listForPark(feedback, page, size);
        return new ResponseEntity<>(new ResultObject(pageResult),HttpStatus.OK);
    }

    @ApiOperation(value = "园区后台用户的反馈列表查询", notes = "")
    @PostMapping("/listForParkUser/{page}/{size}")
    public ResponseEntity<ResultObject<Feedback>> listForParkUser( @PathVariable("page") int page, @PathVariable("size") int size){
        Feedback feedback = new Feedback();
        feedback.setUserId(myHeaderUtil.getHeaderUser());
        PageResult<FeedbackMobileVO> pageResult = feedbackService.listForUser(feedback, page, size);
        return new ResponseEntity<>(new ResultObject(pageResult),HttpStatus.OK);
    }

    @ApiOperation(value = "云和后台分页查询", notes = "content：反馈内容；projectId：反馈项目；replyStat：回复状态1：已回复，0未回复；" +
            "userType：company表示小程序提交的，park表示app提交的")
    @PostMapping("/listForYunhe/{page}/{size}")
    public ResponseEntity<ResultObject<Feedback>> listForYunhe(@RequestBody Feedback feedback, @PathVariable("page") int page, @PathVariable("size") int size){
        PageResult<Feedback> pageResult = feedbackService.listForPark(feedback, page, size);
        ResultObject res = new ResultObject(pageResult);
        return new ResponseEntity<>(res,HttpStatus.OK);
    }

    @ApiOperation(value = "移动端分页查询（path形式）", notes = "")
    @PostMapping("/listForMobile/{page}/{size}/{userId}")
    public ResponseEntity<ResultObject<Feedback>> listForMobile(@PathVariable("userId") Long userId, @PathVariable("page") int page, @PathVariable("size") int size){
        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        PageResult<FeedbackMobileVO> pageResult = feedbackService.listForUser(feedback, page, size);
        return new ResponseEntity<>(new ResultObject(pageResult),HttpStatus.OK);
    }
    @ApiOperation(value = "移动端分页查询（body形式）")
    @PostMapping("/listForMobileV2")
    public ResponseEntity<ResultObject<Feedback>> listForMobileV2(@RequestBody FeedbackQueryCondition condition){
        Feedback feedback = new Feedback();
        feedback.setUserId(condition.getUserId());
        PageResult<FeedbackMobileVO> pageResult = feedbackService.listForUser(feedback, condition.getPage(), condition.getSize());
        return new ResponseEntity<>(new ResultObject(pageResult),HttpStatus.OK);
    }

    @ApiOperation(value = "反馈详情查看 包括回复列表", notes = "")
    @GetMapping("/detail/{id}")
    public ResponseEntity<ResultObject<Feedback>> detail(@PathVariable("id") long id){
        JSONObject jsonObject = feedbackService.detail(id);
        return new ResponseEntity<>(new ResultObject(jsonObject),HttpStatus.OK);
    }


}